from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
import time, random
from MyAutomator import MyAutomation

class DongFangTouTiao(MyAutomation):

    def __init__(self, APP_PACKAGE, APP_ACTIVITY):

        # floaticon
        self.coin_float_ele = 'cn.youth.news:id/m1'#m5 新   m1 老

        # read
        self.subject = '//*[@text="上海"]'
        self.list_item_ele = 'com.ss.android.article.lite:id/bz'


        # 全局
        # 推送新闻框的忽略按钮
        self.ignore_push_news = 'com.songheng.eastnews:id/ea'
        # 升级框关闭叉叉
        self.close_update = 'com.songheng.eastnews:id/pj'

        super().__init__(APP_PACKAGE, APP_ACTIVITY)

    # read
    def read_article(self, interval):
        time.sleep(3)
        suject = self.wait.until(EC.presence_of_element_located((By.XPATH, self.subject)))
        suject.click()
        # self.swipeDown()
        time.sleep(3)

        start = time.time()
        while True:

            for i in range(random.randint(2, 3)):
                time.sleep(random.randint(1, 3))
                print('列表向下滑动')
                try:
                    self.swipeUp()
                except:
                    print('滑动异常')

            try:
                articles = self.wait.until(EC.presence_of_all_elements_located((By.ID, self.list_item_ele)))
                print('新闻数量', len(articles))
                for article in articles:
                    try:
                        article.click()
                        print('进入新闻')
                        try:
                            # for i in range(random.randint(7, 9)):
                            #     time.sleep(random.randint(5, 6))
                            #     self.swipeUp()

                            for i in range(random.randint(5, 6)):
                                time.sleep(random.randint(5, 6))
                                self.swipeUp()
                        except :
                            print('在详情页报错，应直接退出')

                        # 返回
                        self.driver.press_keycode(4)
                        print('退出详情,回到列表1')


                    except:
                        print('退出详情,回到列表2')
                        # self.swipeUp()
            except:
                print('报错')

            print(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())))

            end = start + interval
            now = time.time()
            if now >= end:
                print('结束新闻循环')
                break
            else:
                print('未到结束时间')





    # 入口函数
    def main(self):
        print('打开 {} App ...'.format('toutiao'))
        # func_list = ['smallvideo', 'read_article', 'video']  # 新闻阅读，小视频，视频三个方法随机
        func_list = ['read_article']
        for i in range(12):
            exp = 'self.{}({})'.format(random.choice(func_list), random.randint(2000, 5000))
            print(exp)
            eval(exp)
            print('App {} 今日任务完成！'.format('toutiao'))
            # home键
            self.driver.press_keycode(3)
            self.driver.close()



#zhongqing
qutoutiao_appPackage = 'com.ss.android.article.lite'
qutoutiao_appActivity = 'com.ss.android.article.lite.activity.SplashActivity'


if __name__ == '__main__':
    qutoutiao = DongFangTouTiao(qutoutiao_appPackage, qutoutiao_appActivity)
    qutoutiao.main()